def precise_calculation(total_pens):
    # 更精确的计算方法
    max_ewes = 0
    best_rams = 0
    max_lambs = 0
    
    for num_ewes in range(100, 1000):
        num_rams = max(1, math.ceil(num_ewes / 50))
        
        # 计算各阶段同时存在的羊栏需求
        # 1. 交配组数
        mating_groups = math.ceil(num_ewes / 14)
        
        # 2. 孕期组数
        pregnancy_groups = math.ceil(num_ewes / 8)
        
        # 3. 哺乳期组数
        lactation_groups = math.ceil(num_ewes / 6)
        
        # 4. 空怀期组数
        rest_groups = math.ceil(num_ewes / 14)
        
        # 5. 公羊栏
        ram_pens = math.ceil(num_rams / 4)
        
        # 6. 育肥栏 - 需要计算重叠的育肥批次
        # 每批羔羊数量
        lambs_per_batch = num_ewes * lambs_per_birth
        # 育肥栏批次
        fattening_batches = math.ceil(fattening_duration / (reproduction_cycle / mating_groups))
        fattening_pens = math.ceil(lambs_per_batch / 14) * fattening_batches
        
        # 最大同时需要的羊栏数
        peak_pens = max(mating_groups, pregnancy_groups, lactation_groups, rest_groups) + ram_pens + fattening_pens
        
        if peak_pens <= total_pens:
            max_ewes = num_ewes
            best_rams = num_rams
            max_lambs = num_ewes * lambs_per_birth * (365 / reproduction_cycle)
        else:
            break
    
    return max_ewes, best_rams, max_lambs

# 使用更精确的方法计算
precise_ewes, precise_rams, precise_lambs = precise_calculation(total_pens)
print("\n更精确计算结果:")
print(f"基础母羊数量: {precise_ewes}只")
print(f"种公羊数量: {precise_rams}只")
print(f"年出栏量: {int(precise_lambs)}只")

# 计算1500只年出栏的羊栏缺口
required_ewes_1500 = math.ceil(1500 / (lambs_per_birth * (365 / reproduction_cycle)))
scale = required_ewes_1500 / precise_ewes
required_pens_1500 = total_pens * scale
deficit_1500 = math.ceil(required_pens_1500 - total_pens)
print(f"\n要达到年出栏1500只:")
print(f"需要基础母羊约{required_ewes_1500}只")
print(f"需要羊栏约{math.ceil(required_pens_1500)}个")
print(f"现有112个羊栏的缺口约为{deficit_1500}个")
